import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';

/**
 * version 0.1.1 2018/5/2
 * version 0.1.2 2018/7/19
 * version 0.1.3 2018/10/15
 * version 0.1.4 2020/10/15 移植到新项目
 * 统一为get put post delete 四个请求带上access_token
 * version 0.1.4 2020/10/23 重构为Async Await 调用的方式
 */
@Injectable({
  providedIn: 'root'
})
export class NetService {

  private JSON_TYPE = 'application/json;charset=UTF-8'

  private FORM_TYPE = 'application/x-www-form-urlencoded;charset=UTF-8'

  constructor(private http: HttpClient) { }

  getType(type: string) {
    if (type === null || type === undefined) {
      return this.JSON_TYPE;
    } else {
      if (type === 'json') {
        return this.JSON_TYPE;
      } else if (type === 'form') {
        return this.FORM_TYPE;
      }
    }
  }


  get(path: string, content_type?: string): Observable<any> {
    const token = this.getActiveToken();
    const type = this.getType(content_type);
    return this.http
      .get(path, {
        headers: {
          ['Content-Type']: [type],
          // ['Authorization']: ['Bearer ' + token]
        }
      });
  }

  getAsync(url: string, params?: Object, content_type?: string): Promise<any> {
    let httpParams = new HttpParams();
    if (params) {
      for (const key in params) {
        if (params[key] === false || params[key]) {
          httpParams = httpParams.set(key, params[key]);
        }
      }
    }
    const type = this.getType(content_type);
    return this.http
      .get(url, {
        headers: {
          ['Content-Type']: [type],
          // ['Authorization']: ['Bearer ' + token]
        }
      }).toPromise();
  }

  put(path: string, _params: any | null, content_type?: string): Observable<any> {
    const token = this.getActiveToken();
    const type = this.getType(content_type);
    return this.http
      .put(path, _params, {
        headers: {
          ['Content-Type']: [type],
          // ['Authorization']: ['Bearer ' + token]
        }
      });
  }

  putAsync(url: string, params?: Object, content_type?: string): Promise<any> {
    const type = this.getType(content_type);
    return this.http
      .put(url, params, {
        headers: {
          ['Content-Type']: [type],
          // ['Authorization']: ['Bearer ' + token]
        }
      }).toPromise();
  }

  post(path: string, _params: any | null, content_type?: string): Observable<any> {
    const token = this.getActiveToken();
    const type = this.getType(content_type);
    return this.http
      .post(path, _params, {
        headers: {
          ['Content-Type']: [type],
          // ['Authorization']: ['Bearer ' + token]
        }
      }
      );
  }

  postAsync(url: string, params?: Object, content_type?: string): Promise<any> {
    const type = this.getType(content_type);
    return this.http
      .post(url, params, {
        headers: {
          ['Content-Type']: [type],
          // ['Authorization']: ['Bearer ' + token]
        }
      }).toPromise();
  }

  postForm(path: string, _params: any | null): Observable<any> {
    const token = this.getActiveToken();

    let httpParams = new HttpParams();
    if (_params) {
      for (const key in _params) {
        // _params[key] === false ??
        if (_params[key]) {
          httpParams = httpParams.set(key, _params[key])
        }
      }
    }
    return this.http
      .post(path, httpParams, {
        headers: {
          ['Content-Type']: [this.FORM_TYPE],
          // ['Authorization']: ['Bearer ' + token]
        }
      }
      );
  }

  postFormAsync(path: string, _params: any | null): Promise<any> {
    const token = this.getActiveToken();

    let httpParams = new HttpParams();
    if (_params) {
      for (const key in _params) {
        // _params[key] === false ??
        if (_params[key]) {
          httpParams = httpParams.set(key, _params[key])
        }
      }
    }
    return this.http
      .post(path, httpParams, {
        headers: {
          ['Content-Type']: [this.FORM_TYPE],
          // ['Authorization']: ['Bearer ' + token]
        }
      }
      ).toPromise();
  }



  delete(path: string, _params: any | null, content_type?: string): Observable<any> {
    const token = this.getActiveToken();
    const type = this.getType(content_type);
    return this.http.request('DELETE', path, {
      headers: new HttpHeaders({
        'Content-Type': type,
        // 'Authorization': 'Bearer ' + token,
      }),
      body: _params
    });

  }

  deleteAsync(url: string, params?: Object, content_type?: string): Promise<any> {
    const type = this.getType(content_type);
    return this.http.request('DELETE', url, {
      headers: new HttpHeaders({
        'Content-Type': type,
        // 'Authorization': 'Bearer ' + token,
      }),
      body: params
    }).toPromise();
  }

  uploadImg(path: string, _params: any | null): Observable<any> {
    return this.http
      .post(path, _params, { responseType: 'text' }
      );
  }

  // 20181214 根据新版的后端登录逻辑开始
  getActiveToken() {
    return "token";
  }


}
